package com.example.coolshark.controller;

import com.example.coolshark.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${dirPath}")
    private String dirPath;
    @Autowired(required = false)
    BannerMapper mapper;

    @RequestMapping("/upload")
    public String upload(MultipartFile pic) throws IOException {
        //得到唯一文件名
        String fileName = pic.getOriginalFilename();
        String suffix= fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        //准备报存的文件夹
     /*   String dirPath="E:/xiangmu/myjsd07-03/coolshark/files";*/
        File dirFile = new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        //保存图片到指定文件夹
        String filePath = dirPath+"/"+fileName;
        pic.transferTo(new File(filePath));
       /* mapper.insert("/"+fileName);*/
        return "/"+fileName;

    }
    @RequestMapping("/remove")
    public void remove(String url){
//        mapper.deleteByUrl(url);
//        new File("E:/xiangmu/myjsd07-03/coolshark/files"+url).delete();
        new File(dirPath+url).delete();



    }
}
